package com.wit.dao.impl;

import hibernate.HibernateSessionFactory;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;

import com.wit.dao.PolicyDao;
import com.wit.po.Client;
import com.wit.po.Company;
import com.wit.po.Department;
import com.wit.po.Employee;
import com.wit.po.Policy;

public class PolicyDaoImpl implements PolicyDao {

	
	public boolean addPolicy(Policy policy) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.save(policy);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public boolean deletePolicy(Policy policy) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.delete(policy);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public List<Policy> findPolicyByCondition(DetachedCriteria dc,
			Integer startRecord, Integer pageSize) {
		List<Policy> compList = new ArrayList<Policy>();
		try{
			compList = (List<Policy>)dc.getExecutableCriteria(HibernateSessionFactory.getSession()).setFirstResult(startRecord).setMaxResults(pageSize).list();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return compList;
	}

	
	public Policy findPolicyByIdenfier(String policyId) {
		Session s = null;
		Policy policy = null;
		try{
			s = HibernateSessionFactory.getSession();
			List<Policy> compList = (List<Policy>)s.createQuery("FROM Policy WHERE policyId = ?").setString(0, policyId).list();
			if(compList.size() == 1){
				policy = compList.get(0);
			}
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return policy;
	}

	
	public Integer findPolicyCount(DetachedCriteria dc) {
		List<Integer> countList = new ArrayList<Integer>();
		try{
			countList = (List<Integer>)dc.getExecutableCriteria(HibernateSessionFactory.getSession()).list();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return countList.get(0);
	}

	
	public boolean modifyPolicy(Policy policy) {
		Session s = null;
		Transaction tx = null;
		boolean isFinished = false;
		try{
			s = HibernateSessionFactory.getSession();
			tx =  s.beginTransaction();
			s.update(policy);
			tx.commit();
			s.close();
			isFinished = true;
		}catch(HibernateException e){
			tx.rollback();
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return isFinished;
	}

	
	public Client findClientById(String clientId) {
		Session s = null;
		Client client = null;
		try{
			s = HibernateSessionFactory.getSession();
			List<Client> compList = (List<Client>)s.createQuery("FROM Client WHERE clientId = ?").setString(0, clientId).list();
			if(compList.size() == 1)
				client = compList.get(0);
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return client;
	}

	
	public Employee findEmployeeById(String employeeId) {
		Session s = null;
		Employee employee = null;
		try{
			s = HibernateSessionFactory.getSession();
			List<Employee> compList = (List<Employee>)s.createQuery("FROM Employee WHERE employeeId = ?").setString(0, employeeId).list();
			if(compList.size() == 1)
				employee = compList.get(0);
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return employee;
	}

	
	public List<Department> findAllDepartment() {
		Session s = null;
		List<Department> departList = null;
		try{
			s = HibernateSessionFactory.getSession();
			departList = (List<Department>)s.createQuery("FROM Department").list();
			s.close();
		}catch(HibernateException e){
			e.printStackTrace();
		}finally{
			if(s.isOpen())
				s.close();
		}
		return departList;
	}


	public List assessPolicy(DetachedCriteria dc) {
		List compList = null;
		try{
			compList = (List<Policy>)dc.getExecutableCriteria(HibernateSessionFactory.getSession()).list();
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return compList;
	}
}
